﻿using System;
using System.Windows.Forms;
using DbManager;
using DbManager.DataManager;
using DbManager.QueryBuilder.DbEntity;

namespace TAProject.UI.Service.Transport
{
    public partial class FormTransportEdit : FormTemplate
    {
        private readonly Form _parentForm;
        private readonly IDataManager _dataManager;
        private readonly IConnectionManager _connetctionManager;
        private readonly AEntityTransport _aEntityTransport;
        private readonly bool _isUpdate;

        public FormTransportEdit(Form parentForm, IDataManager dataManager, IConnectionManager connetctionManager, AEntityTransport aEntityTransport, bool isUpdate)
            : base(parentForm)
        {
            InitializeComponent();

            _parentForm = parentForm;
            _dataManager = dataManager;
            _connetctionManager = connetctionManager;
            _aEntityTransport = aEntityTransport;
            _isUpdate = isUpdate;

            textBoxPerevozshik.Text = _aEntityTransport.Perevozshik;
            textBoxTelPerevozshik.Text = _aEntityTransport.TelephonePrevozshika;
            textBoxVoditel.Text = _aEntityTransport.Voditel;
            textBoxTelVoditel.Text = _aEntityTransport.TelephoneVoditelia;
            textBoxNomer.Text = _aEntityTransport.Nomer;
            textBoxCar.Text = _aEntityTransport.Car;
            numericUpDownPlace.Value = (_aEntityTransport.Places == 0) ? 1 : _aEntityTransport.Places;
            textBoxDescription.Text = _aEntityTransport.Description;

            _parentForm.Enabled = false;
        }

        private void FormClose()
        {
            _parentForm.Enabled = true;
            Close();
        }

        private void ButtonCancelClick(object sender, EventArgs e)
        {
            FormClose();
        }

        private void ButtonSubmitClick(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(textBoxPerevozshik.Text) &&
                !string.IsNullOrEmpty(textBoxTelPerevozshik.Text) &&
                !string.IsNullOrEmpty(textBoxVoditel.Text) &&
                !string.IsNullOrEmpty(textBoxTelVoditel.Text) &&
                !string.IsNullOrEmpty(textBoxNomer.Text) &&
                !string.IsNullOrEmpty(textBoxCar.Text) &&
                !string.IsNullOrEmpty(textBoxDescription.Text))
            {
                _aEntityTransport.Perevozshik = textBoxPerevozshik.Text;
                _aEntityTransport.TelephonePrevozshika = textBoxTelPerevozshik.Text;
                _aEntityTransport.Voditel = textBoxVoditel.Text;
                _aEntityTransport.TelephoneVoditelia = textBoxTelVoditel.Text;
                _aEntityTransport.Nomer = textBoxNomer.Text;
                _aEntityTransport.Car = textBoxCar.Text;
                _aEntityTransport.Places = (int) numericUpDownPlace.Value;
                _aEntityTransport.Description = textBoxDescription.Text;

                var rowAffected = _dataManager.ManageDataInDb(
                    _isUpdate ? _aEntityTransport.GetUpdateQuery() : _aEntityTransport.GetInsertQuery(),
                    _connetctionManager.GetConnection());
                if (rowAffected == 0)
                {
                    MessageBox.Show("Ошибка добавления данных в БД.");
                }
                else
                {
                    FormClose();
                }
            }
            else
            {
                MessageBox.Show("Проверьте правильность заполнения полей.");
            }
        }

        private void NumericUpDownPlaceValueChanged(object sender, EventArgs e)
        {

        }
    }
}